import javax.naming.InsufficientResourcesException;
import java.util.*;

public class TestBinaryTree {
    static class TreeNode {
        public char val;//数据值
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        //E.right = H;
        //this.root = A;
        return A;
    }

    /**
     * 递归:前序遍历  根 左 右
     */
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    /**
     * 非递归: 前序遍历
     */
    public void preOrderNor(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNode cur = root;
        Deque<TreeNode> stack = new ArrayDeque<>();
        while (cur != null && !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.println(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }
    /**
     * 力扣: 前序遍历
     */
    /*List<Integer> ret = new ArrayList<>();
    public List<Integer> preorderTraversal(TreeNode root) {
        if (root == null) {
            return ret;
        }
        ret.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return ret;
    }*/

    /*public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        func(root,ret);
        return ret;
    }

    public void func(TreeNode root,List<Integer> ret) {
        if (root == null) {
            return;
        }
        //System.out.print(root.val+" ");
        ret.add(root.val);
        func(root.left, ret);
        func(root.right, ret);
    }*/

    //需要接收返回值
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        System.out.println(root.val + "");
        ret.add((int) root.val);
        List<Integer> leftTree = preorderTraversal(root.left);
        ret.addAll(leftTree);
        List<Integer> rightTree = preorderTraversal(root.right);
        ret.addAll(rightTree);
        return ret;
    }
    /**
     * 中序遍历 : 左 根 右
     */
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    /**
     * 非递归: 中序遍历
     */
    public void inOrderNor(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNode cur = root;
        Deque<TreeNode> stack = new ArrayDeque<>();
        while (cur != null && !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.println(top.val + " ");
            cur = top.right;
        }
    }

    /**
     * 后序遍历  左 右 根
     */
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }
    /**
     * 非递归 : 后序遍历
     */
    public void postOrderNor(TreeNode root) {
        if(root == null) {
            return;
        }
        TreeNode cur = root;
        TreeNode prev = null;
        Deque<TreeNode> stack = new ArrayDeque<>();
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if(top.right == null || top.right == prev) {
                System.out.print(top.val+" ");
                stack.pop();
                prev = top;
            }else {
                cur = top.right;
            }
        }
        System.out.println();
    }
    /**
     * 获取二叉树中有多少个节点
     * 时间复杂度 : O(n)
     * 空间复杂度 : O(log② N)
     */
    //1.左子树的节点 + 右子数的节点 + 1
    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftSize = size(root.left) ;
        int rightSize = size(root.right);
        return leftSize + rightSize + 1;
    }
    //2.遍历的方式
    //root 不为空时 , nodesize ++
    public static int nodeSize;
    public void size2(TreeNode root) {
        if (root == null) {
            return;
        }
        nodeSize++;
        size2(root.left);
        size2(root.right);

    }


    // 获取叶子节点的个数
    public int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
     if (root.left == null && root.right == null) {
         return 1;
     }
     int leftSize = getLeafNodeCount(root.left);
     int rightSize = getLeafNodeCount(root.right);
     return leftSize+rightSize;
    }

    // 子问题思路-求叶子结点个数
    // 获取第K层节点的个数
    //相当于左树的第 k-1 层 加上 右树的第 k-1 层

    public int getKLevelNodeCount(TreeNode root,int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        int leftSize = getKLevelNodeCount(root.left,k-1);
        int rightSize = getKLevelNodeCount(root.right,k-1);
        return leftSize+rightSize;
    }

    // 获取二叉树的高度
    // 左树的高度和右树的高度的最大值 + 1
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHight = getHeight(root.left);
        int rightHight = getHeight(root.right);
        return (leftHight > rightHight) ? (leftHight+1) : (rightHight+1);
    }


    /**检测值为value的元素是否存在
     * 前序遍历 :先遍历根节点 , 然后遍历左子树 , 再遍历右子树
     * @param root
     * @param val
     * @return
     */
    public TreeNode find(TreeNode root, int val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode leftTree = find(root.left,val);
        if (leftTree != null) {
            return leftTree;
        }
        TreeNode rightTree = find(root.right,val);
        if (rightTree != null) {
            return rightTree;
        }
        return null;
    }
    /**给你两棵二叉树的根节点 p 和 q ，编写一个函数来检验这两棵树是否相同。
     * 如果两个树在结构上相同，并且节点具有相同的值，则认为它们是相同的。
     * 思路 :结构相同 , val 值相同
     * 1.一个为空 , 一个不为空 , 不是
     * 2.两个都空 , 是
     * 3.两个都不为空 , 不一定
     */
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }else if ((p == null && q != null) || (p != null && q == null)) {
            return false;
        }else if (q.val != p.val) {
            return false;
        }
        /*boolean ret1 = isSameTree(p.left,q.left);
        boolean ret2 = isSameTree(p.right,q.right);
        return ret1 && ret2;*/
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);

    }
    /**
     * 给你两棵二叉树 root 和 subRoot 。检验 root 中是否包含和 subRoot 具有相同结构和节点值的子树。
     * 如果存在，返回 true ；否则，返回 false 。
     * 二叉树 tree 的一棵子树包括 tree 的某个节点和这个节点的所有后代节点。tree 也可以看做它自身的一棵子树。
     * 1.是不是相同的数
     * 2.是不是root左子树
     * 3.是不是root右子数
     * 时间复杂度 :
     */
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (isSameTree(root,subRoot)) {
            return true;
        }else {
            if (root != null) {
                return isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);
            }else {
                return false;
            }
        }

    }
    /**
     * 给你一棵二叉树的根节点 root ，翻转这棵二叉树，并返回其根节点。
     */
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
    /**
     *给定一个二叉树，判断它是否是高度平衡的二叉树
     */
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        int leftH = getHeight(root.left);
        int riggtH = getHeight(root.right);
        return Math.abs(leftH - riggtH) < 2 && isBalanced(root.left) && isBalanced(root.right);
    }
    /**
     * 对称二叉树
     * 判断这棵树的左子树 和右子树 是不是对称的
     */
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isSymmetricChild(root.left,root.right);

    }
    public boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree) {
        if (leftTree ==null &&  rightTree == null) {
            return true;
        }
        if (leftTree != null && rightTree == null || leftTree == null && rightTree != null) {
            return false;
        }
        if (leftTree.val != rightTree.val) {
            return false;
        }
        return isSymmetricChild(leftTree.left,rightTree.right)
                && isSymmetricChild(leftTree.right,rightTree.left);
    }
    /**
     * 二叉树的层序遍历
     */
    public void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.println(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
    public List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> tmp = new ArrayList<>();
            while (size != 0) {
                TreeNode cur = queue.poll();
                //System.out.println(cur.val + " ");
                tmp.add((int) cur.val);
                size--;
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            list.add(tmp);
        }
        return list;
    }

    /**
     * 判断一棵树是不是完全二叉树
     * 用队列的方式;依次入队列 ,
     * 每次从队列中取出一个节点:
     *  为空 : 开始判断队列中剩余元素
     *  不为空 : 把左子树和右子树带进来
     * @param root
     * @return
     */
    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if (root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur != null) {
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }

        }

        while (!queue.isEmpty()) {
            TreeNode tmp = queue.poll();
            if (tmp != null) {
                return false;
            }
        }
        return true;
    }


}
